from operator import itemgetter
from itertools import groupby

# 一个字典或者实例的序列，然后想根据某个特定的字段，来分组迭代访问
rows = [
    {'address': 'AA', 'date': '07/01/2012'},
    {'address': 'BB', 'date': '07/04/2012'},
    {'address': 'CC', 'date': '07/02/2012'}
]

def demo_1():
    rows.sort(key=itemgetter('date'))
    # groupby函数根据key属性进行元素排序
    for date, items in groupby(rows, key=itemgetter('date')):
        print(date)
        for l in items:
            print(' ', l)

if __name__ == '__main__':
    demo_1()